package com.tgy.leetcode;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @Author: tgy
 * @Date: 12/9/20 9:50 AM
 *
 * https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/
 */
public class _121_买卖股票的最佳时机_02 {

    public int maxProfit(int[] prices) {

        if (prices == null || prices.length <= 1) {

            return 0;
        }


        int[] divs = new int[prices.length - 1];

        for (int i = 1; i < prices.length; i++) {

            divs[i - 1] = prices[i] - prices[i - 1];
        }

        int maxProfix,preSum;

        maxProfix = preSum = divs[0];

        for (int i = 1; i < divs.length; i++) {

            if (preSum > 0) {

                preSum = preSum + divs[i];
            }else {

                preSum = divs[i];
            }

            if (preSum > maxProfix) {

                maxProfix = preSum;
            }
        }

        return maxProfix > 0? maxProfix:0;
    }
}
